package com.warom.sdg.util;

import android.util.Log;

/**
 * 串口参数类，用于配置j2mod库中的SerialConnection
 * 继承自j2mod库的SerialParameters类，确保类型兼容
 */
public class SerialParameters extends com.ghgande.j2mod.modbus.util.SerialParameters {
    private static final String TAG = "SerialParameters";
    
    // 串口常量定义，与j2mod库保持一致
    public static final int DATABITS_5 = 5;
    public static final int DATABITS_6 = 6;
    public static final int DATABITS_7 = 7;
    public static final int DATABITS_8 = 8;
    
    public static final int STOPBITS_1 = 1;
    public static final int STOPBITS_2 = 2;
    
    public static final int PARITY_NONE = 0;
    public static final int PARITY_ODD = 1;
    public static final int PARITY_EVEN = 2;
    
    // 额外的参数，j2mod可能不直接支持
    private int receiveTimeout = 1000;
    
    /**
     * 构造函数
     * @param portName 串口名称
     * @param baudRate 波特率
     */
    public SerialParameters(String portName, int baudRate) {
        super();
        setPortName(portName);
        setBaudRate(baudRate);
        setDatabits(DATABITS_8);
        setStopbits(STOPBITS_1);
        setParity(PARITY_NONE);
        // 超时时间在SerialConnection中设置
        setEncoding("rtu");
        setEcho(false);
    }
    
    /**
     * 设置超时时间
     * @param timeout 超时时间（毫秒）
     */
    public void setReceiveTimeout(int timeout) {
        this.receiveTimeout = timeout;
    }
    
    /**
     * 获取超时时间
     * @return 超时时间（毫秒）
     */
    public int getReceiveTimeout() {
        return receiveTimeout;
    }
    
    /**
     * 转换为j2mod库识别的校验位格式
     * @return j2mod库识别的校验位
     */
    public int getJ2modParity() {
        switch (getParity()) {
            case PARITY_ODD:
                return 1; // j2mod奇校验
            case PARITY_EVEN:
                return 2; // j2mod偶校验
            case PARITY_NONE:
            default:
                return 0; // j2mod无校验
        }
    }
} 